package com.scalar.chiptrack.adaptor.ikanos.gtc;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.users.VendorAdaptor;
import com.scalar.chiptrack.users.dao.AdaptorSetUpDAO;

import javax.mail.Message;

/**
 * User: ChandraSekhar
 * Date: Jul 15, 2008
 * Time: 12:12:43 PM
 */
public class GTCPOP3Adaptor extends JobHandler implements AdaptorConstants
{
    private static final String HOST                    = "mail.scalarsoft.com";

    public static final String FILE_NAME_CP_WIP 		= "IKANOS_CP_WIP_REPORT";
    public static final String FILE_NAME_FT_WIP 	    = "IKANOS_FT_WIP_REPORT";
    //public static final String FILE_NAME_FT_INVENTORY 	= "IKANOS_FT_INV_REPORT";
    //public static final String FILE_NAME_CP_BINNING 	= "SORT_BIN_DATA";
    public static final String FILE_NAME_CP_BINNING 	= "IKANOS_CPDAILYREPORT";
    public static final String FILE_NAME_CP_SHIP 	    = "IKANOS_SHIPPING_CP_ALERT";
    public static final String FILE_NAME_FT_SHIP 	    = "IKANOS_SHIPPING_FT_ALERT";
    public static final String FILE_NAME_DAILY_YIELD 	= "IKANOS FT DAILY YIELD REPORT";
    public static final String FILE_NAME_FG_WIP 	    = "IKANOS_INV_REPORT";

    public static final String ADAPTOR_NAME             = "GTC_WIP";

    private static final String SUBJECT_CP_WIP          = "IKANOS CP WIP Report";
    private static final String SUBJECT_FT_WIP          = "IKANOS FT WIP Report";
    private static final String SUBJECT_FG_WIP          = "FG inventory";
    //private static final String SUBJECT_CP_BINNING      = "IKANOS CP DATA";
    private static final String SUBJECT_CP_BINNING      = "IKANOS S/W Bin Report";

    private static final String SUBJECT_CP_SHIP         = "IKANOS CP Shipping Alert Report";
    private static final String SUBJECT_FT_SHIP         = "IKANOS FT Shipping Alert Report";
    private static final String SUBJECT_FT_YIELD        = "IKANOS FT Daily Yield Report";

    private Logger m_adaptorLogger      = null;
    private String m_dataOutputDir      = null;
    private String m_username           = null;
    private String m_password           = null;
    private int    m_adaptor_id         = -1;

    public GTCPOP3Adaptor() throws Exception
    {
        try
        {
            VendorAdaptor vendorAdaptor = AdaptorSetUpDAO.getVendorAdaptorDetailsByVendorNameAndLocation( VENDOR_NAME_GTC, LOCATION_TYPE_TEST);

            m_username = vendorAdaptor.getUserId().trim();
            m_password = vendorAdaptor.getPassWord().trim();
            m_dataOutputDir = vendorAdaptor.getOutPutDirectory().trim();

            if (m_dataOutputDir != null)
            {
                if (m_dataOutputDir.endsWith("/") || m_dataOutputDir.endsWith("\\"))
                {
                    m_dataOutputDir = m_dataOutputDir + m_username;
                }
                else
                {
                    m_dataOutputDir = m_dataOutputDir + "/" + m_username;
                }
                AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
                m_adaptorLogger = AdaptorUtils.getAdaptorLogger( m_dataOutputDir, VENDOR_NAME_GTC );
                AdaptorUtils.setLogger( m_adaptorLogger );
            }
        }
        catch (Exception se)
        {
            log("ERROR!! ERROR!! ERROR!! Exception while loading GTC Adaptor Username, Password details from database, Exception: " + se);
            throw se;
        }
    }

    //Main Method
    public static void main(String[] args) throws Exception
    {
        new GTCPOP3Adaptor().execute();
    }

    /**
     *
     * @throws Exception
     */
    public void execute() throws Exception
    {
        try
        {
            m_adaptor_id = AdaptorRunInfo.saveAdaptorLastUpdatedStatus( CONSTANT_ADAPTOR, ADAPTOR_NAME, CONSTANT_SUCCESS, -1);
            // Step 1
             // prints the version of Adaptor
            AdaptorUtils.printHeader( VENDOR_NAME_GTC, CONSTANT_ADAPTOR );

            // Step 2
            // removes the old files out side done directory
            AdaptorUtils.removeOldFiles(m_dataOutputDir, m_adaptorLogger);
            // Step 3
            //Retrive All the Messages
            Message[] messages = AdaptorUtils.getAllPOP3InboxMessages( HOST, m_username, m_password);
            // Step 4
            // Retrieve & Saves the latest message
            AdaptorUtils.readLatestMessage( m_adaptor_id, messages, SUBJECT_CP_WIP,    m_dataOutputDir, FILE_EXTN_XLS, null );
            AdaptorUtils.readLatestMessage( m_adaptor_id, messages, SUBJECT_FT_WIP,  m_dataOutputDir, FILE_EXTN_CSV, null );
            //AdaptorUtils.readLatestMessage( m_adaptor_id, messages, SUBJECT_FG_WIP,  m_dataOutputDir, FILE_EXTN_CSV, null );
            //AdaptorUtils.readLatestDayMessages( m_adaptor_id, messages, SUBJECT_CP_BINNING,   m_dataOutputDir, FILE_EXTN_TXT, FILE_NAME_CP_BINNING );
            AdaptorUtils.readLatestMessage( m_adaptor_id, messages, SUBJECT_CP_BINNING,   m_dataOutputDir, FILE_EXTN_XLS, null );
            AdaptorUtils.readLatestMessage( m_adaptor_id, messages, SUBJECT_CP_SHIP,   m_dataOutputDir, FILE_EXTN_CSV, null );
            AdaptorUtils.readLatestMessage( m_adaptor_id, messages, SUBJECT_FT_SHIP,   m_dataOutputDir, FILE_EXTN_CSV, null );
            AdaptorUtils.readLatestMessage( m_adaptor_id, messages, SUBJECT_FT_YIELD,   m_dataOutputDir, FILE_EXTN_XLS, null );


        }
        catch (Exception e)
        {
            AdaptorUtils.removeOldFiles(m_dataOutputDir, m_adaptorLogger);
            AdaptorRunInfo.updatedAdaptorStatus( CONSTANT_FAILURE, m_adaptor_id);
            System.out.println("Exception while trying to connect and read data from " + HOST +" site: " + e);
            log("Exception while trying to connect and read data from " + HOST +" site: " + e);
            e.printStackTrace();
            throw e;
        }
    }

    /**
     *
     * @param message
     */
    private void log(String message)
    {

        if (message == null) return;

        if (m_adaptorLogger != null)
        {
            synchronized (m_adaptorLogger)
            {
                m_adaptorLogger.logDebug(message + LINE_SEPARATOR);
            }
        }
        else
        {
            System.out.println(message);
        }
    }

}